<?	/*
	// File:	mysql.wcp.phps
	// Purpose:	MySQL database update
	// Author:	Felix <webmaster@can-host.com>
	*/


// Handle user manipulation
if ($type == "user") {
	// Create User :: expect *
	if ($data['action'] == "create") {
		// Update DB
		mysql_query("UPDATE users SET action='', time=NOW() WHERE username='".$data['username']."'");
	}
	
	// Update User :: expect *
	if ($data['action'] == "update") {
		// Update DB
		mysql_query("UPDATE users SET username1='".$data['username']."', action='', time=NOW() WHERE username='".$data['username']."'");
	}
	
	// Suspend User :: expect username
	if ($data['action'] == "suspend") {
		// Update DB
		mysql_query("UPDATE users SET suspend='true',action='', time=NOW() WHERE username='".$data['username']."'");
	}
	
	// Unsuspend User :: expect username
	if ($data['action'] == "unsuspend") {
		// Update DB
		mysql_query("UPDATE users SET suspend='false',action='', time=NOW() WHERE username='".$data['username']."'");
	}
	
	// Remove User :: expect username
	if ($data['action'] == "remove") {
		// Remove any MySQL databases left
		mysql_select_db("mysql");
		
		// remove any db operated by that user
		$dbp = mysql_query("SELECT Db FROM db WHERE User='".$data['username']."'");
		while ($dbdata = mysql_fetch_array($dbp))
			mysql_query("DROP DATABASE IF EXISTS `".$dbdata['Db']."`");
		// Remove user privileges
		mysql_query("DELETE FROM user WHERE User='".$data['username']."'");
		mysql_query("DELETE FROM db WHERE User='".$data['username']."'");
		mysql_query("FLUSH PRIVILEGES");
		
		// switch back to webcp's db
		mysql_select_db($cfg['dbname']);
		
		// Update DB
		mysql_query("DELETE FROM users WHERE username='".$data['username']."' and id='".$data['id']."'");
	}
}


// Handle domains manipulation
if ($type == "domain") {

	// Handle Domain Creation :: expect *
	if ($data['action'] == 'create') {
		// Update DB
		if ($data['id'])
			mysql_query("UPDATE domains SET action='', time=NOW() WHERE id='".$data['id']."'");
	}
	
	// Handle Domain updates :: expect *
	elseif ($data['action'] == 'update') {
		// Update DB
		if ($data['id'])
			mysql_query("UPDATE domains SET action='', time=NOW() WHERE id='".$data['id']."'");
	}
	
	// Suspend Domain :: expect 'id'
	elseif ($data['action'] == "suspend") {
		// Update DB
		if ($data['id'])
			mysql_query("UPDATE domains SET suspended = 'true', action='', time=NOW() WHERE id='".$data['id']."'");
	}
	
	// Unsuspend Domain :: expect 'id',
	elseif ($data['action'] == "unsuspend") {
		// Update DB
		if ($data['id'])
			mysql_query("UPDATE domains SET suspended = 'false', action='', time=NOW() WHERE id='".$data['id']."'");
		
	}
	
	// Remove Domain :: expect *
	elseif ($data['action'] == "remove") {
	 	// Update DB
		if ($data['id'])
			mysql_query("DELETE FROM domains WHERE id='".$data['id']."'");
	}
}
?>
